package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.entity.*;
import com.example.service.UserService;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    //使用Redis存储用户token
    @Autowired
    private StringRedisTemplate redisTemplate;

    //用户登录=============================================
    @PostMapping("/login")
    @AutoLog("用户登录系统")
    public Result login(@RequestBody User user){
        User loginUser = userService.login(user);
        String token = loginUser.getToken();
        //保存用户token
        redisTemplate.opsForValue().set( token, JSON.toJSONString(loginUser),2,TimeUnit.HOURS);
        //保存当前登录用户的id
        redisTemplate.opsForValue().set("userid",JSON.toJSONString(loginUser.getUserId()),2,TimeUnit.HOURS);

        return Result.success(loginUser);
    }

    //用户注册================================================
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    //商家注册=====================================================
    @PostMapping("/registershopper/{shopName}")
    public Result registerShopper(@RequestBody User user, @PathVariable String shopName) {
        //先将商家注册的账号密码信息存储到redis中
        redisTemplate.opsForValue().set(user.getAccount(),user.getPassword());
        //再把其余信息作为审核表的一条数据存入数据库
        userService.storagenewshopper(user,shopName);
        return Result.success();
    }

    //邮箱验证======================================================
    @PostMapping("/sendmailcode/{email}")
    public Result checkEmail(@RequestBody String account,@PathVariable String email){
        Map<String,String> parse = (Map<String, String>) JSON.parse(account);
        String useraccount = parse.get("account");
        userService.checkEmail(email,useraccount);
        return Result.success();
    }

    //检查用户输入的验证码与redis存储的是否一致============================================
    @PostMapping("/checkcode/{mailcode}")
    public Result checkCode(@PathVariable String mailcode,@RequestBody User user){
        userService.checkCode(mailcode,user);
        return Result.success();
    }

    //验证码输入正确后重置密码==================================================
    @PostMapping("/resetpwd")
    public Result resetPwd(@RequestBody User user){
        userService.resetpwd(user.getAccount(),user.getPassword());
        return Result.success();
    }

    //管理员界面下的编辑和新增用户信息
    @PostMapping("/addedit")
    @AutoLog("修改用户信息")
    public Result save(@RequestBody User user){

        if (user.getPassword() == null){
            user.setPassword("123456");
        }

        if(user.getUserId() == null){
            userService.add(user);
        } else{
            userService.update(user);
        }
        return Result.success();
    }

    //管理员界面的删除用户信息
    @DeleteMapping("/del/{userId}")
    @AutoLog("删除用户信息")
    public Result delete(@PathVariable Integer userId){
        userService.del(userId);
        return Result.success();
    }

    //管理员界面的用户信息=================================
    @PostMapping("/searchuserinfo")
    public Result searchUser(@RequestBody Params params){
        PageInfo<User> info = userService.searchUser(params);
        return Result.success(info);
    }

    //管理员界面的商家信息==========================================
    @PostMapping("/searchbusinessinfo")
    public Result searchBusiness(@RequestBody Params params){
        PageInfo<User> info = userService.searchBusiness(params);
        return Result.success(info);
    }

    //商家界面的商家店铺的名称信息===================================
    @PostMapping("/loadshop")
    public Result findshop(@RequestBody ShopInfo shopInfo){

        List<ShopInfo> info =  userService.findshop(shopInfo);

        return Result.success(info);
    }

    //商家界面的商家店铺的树苗数量信息================================
    @PostMapping("/loadnum")
    public Result findnum(@RequestBody ShopInfo shopInfo){

        ShopInfo info =  userService.findnum(shopInfo);

        return Result.success(info);
    }

    //管理员界面的审核申请=====================================
    @PostMapping("/findcheckinfo")
    public Result findCheckInfo(@RequestBody Params params){
        PageInfo<CheckInfo> info = userService.findCheckInfo(params);
        return Result.success(info);
    }

    //管理员界面的审核申请=====================================
    @PostMapping("/checkagree/{checkId}")
    public Result checkAgree(@RequestBody User user,@PathVariable Integer checkId){
        userService.checkAgree(user,checkId);
        return Result.success();
    }

    //管理员界面的拒绝商家申请===============================
    @PostMapping("/checkreject/{checkId}")
    public Result checkReject(@RequestBody User user,@PathVariable Integer checkId){
        userService.checkReject(user,checkId);
        return Result.success();
    }



    //更新头像(统一接口)
    @PostMapping("/updateimg")
//    @AutoLog("修改用户头像")
    public Result updateImg(@RequestBody User user){
        userService.updateImg(user);
        return Result.success();
    }

    //重新加载用户界面的用户修改过的个人信息
    @PostMapping("/reloaduser")
    public Result reloadUser(@RequestBody User user){
        User userinfo = userService.reloadUser(user);
        return Result.success(userinfo);
    }

    //加载商家首页系统商家数量============================
    @GetMapping("/loadshoppernumber")
    public Result loadShopperNumber(){
        int shopperNumber = userService.loadShopperNumber();
        return Result.success(shopperNumber);
    }

    //用户页面跟人中心注销用户接口
    @PostMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        userService.deletetUserByUserId(userId);
        return Result.success();
    }


}
